package com.lsj.controller;

import com.lsj.core.page.QueryPage;
import com.lsj.core.page.ResultPage;
import com.lsj.core.response.CommonResult;
import com.lsj.core.util.ExceptionHelperUtil;
import com.lsj.core.validate.Add;
import com.lsj.core.validate.Update;
import com.lsj.dto.sys.DictAddDto;
import com.lsj.dto.sys.DictDeleteDto;
import com.lsj.dto.sys.DictDto;
import com.lsj.dto.sys.DictUpdateDto;
import com.lsj.service.DictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @Author:lin.sj
 * @Data:2021/8/2 21:22
 * @Desc:
 */
@RestController
@RequestMapping("/sys_dict")
@Api(tags = "字典管理")
public class DictController {
    @Autowired
    DictService dictService;


    @PostMapping("/add")
    @ApiOperation(value = "增加字典")
    public CommonResult addDict(@RequestBody @Validated(Add.class) DictAddDto dto){
        DictDto dictDto = dictService.addDict(dto);
        return CommonResult.success(dictDto);
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除字典")
    public CommonResult deleteDict(@RequestBody @Validated DictDeleteDto dto){
        dictService.deleteDict(dto);
        return CommonResult.success();
    }

    @GetMapping("/get")
    @ApiOperation(value = "获取字典")
    public CommonResult getRole(@RequestParam("dictId") Long dictId){
        ExceptionHelperUtil.nullObjException(dictId,"字典ID不能为空");
        return CommonResult.success(dictService.getDictById(dictId));
    }

    @PostMapping("/update")
    @ApiOperation(value = "更新字典")
    public CommonResult updateRole(@RequestBody @Validated(Update.class) DictUpdateDto dto){
        dictService.updateRole(dto);
        return  CommonResult.success();
    }

    @PostMapping("/list")
    @ApiOperation(value = "获取字典列表")
    public CommonResult listDict(@RequestBody QueryPage<DictDto> queryPage){
        ResultPage<DictDto> dictDtoResultPage = dictService.listDict(queryPage);
        return  CommonResult.success(dictDtoResultPage);
    }
}
